<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组</title>
</head>
<body>
    <script>
        // 定义
        let arr = [10, 11];
        console.log("arr：", arr);

        // push 向数组末尾添加一个或多个元素，并返回数组长度
        let len = arr.push(12, 13);
        console.log("length: ", len);
        console.log("arr: ", arr);

        // unshift 向数组开头添加一个或多个元素，并返回数组长度
        len = arr.unshift(8, 9);
        console.log("length: ", len);
        console.log("arr: ", arr);

        // shift 删除数组中的第一个元素，并返回删除元素
        let del = arr.shift();
        console.log("element: ", del);
        console.log("arr: ", arr);
        console.log("length: ", arr.length);

        // pop 删除数组中的最后一个元素，并返回删除元素
        del = arr.pop();
        console.log("element: ", del);
        console.log("arr: ", arr);
        console.log("length: ", arr.length);

        // splice 删除元素，返回被删除元素数组，splice(索引位置，删除数量)
        arr.unshift(1, 2, 3, 4, 5, 6, 7, 8);
        let delArray = arr.splice(3, 6);
        console.log("del elements: ", delArray);
        console.log("arr: ", arr);
        console.log("length: ", arr.length);

        // reverse 颠倒数组元素的顺序
        delArray.reverse();
        console.log("del elements: ", delArray);

        // sort 首字母排序
        let arr2 = ["banana", "apple", "orange"];
        console.log("arr2: ", arr2);
        arr2.sort();
        console.log("sort arr2: ", arr2);

        // sort 数字排序，参数为比较函数，返回值为0或为正位置不变，返回值为负则交换位置
        let arr3 = [5, 20, 13, 1, 4];
        console.log("arr2: ", arr3);
        arr3.sort((num1, num2) => num1 - num2);
        console.log("sort arr2: ", arr3);

        // filter 筛选
        let arr4 = [10, 11, 12, 13, 14, 15];
        console.log("before filter: ", arr4);
        console.log("after filter: ", arr4.filter((value, index) => { return index > 1}));

        // concat 合并
        let arr5 = ["一", "二", "三"];
        console.log(arr5 = arr5.concat([4, 5], 6, 7));

        // for...of 遍历
        for(let item of arr5) {
            console.log("for...of: ", item);
        }

        // forEach 遍历
        arr5.forEach(item => {
            console.log("forEach: ", item);
        });

    </script>
</body>
</html>